<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Movie
 *
 * @property int $id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $name_en
 * @property string $name_zh
 * @property string $playwright
 * @property string $publish_at
 * @property string $grade
 * @property string $length
 * @property string $official_url
 * @property string $detail
 * @property string $key_word
 * @property string $description
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Article[] $articles
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Episode[] $episodes
 * @property-read mixed $coverage_image
 * @property-read mixed $full_name
 * @property-read mixed $score
 * @property-read mixed $site_url
 * @property-read mixed $urls
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Image[] $images
 * @property-read \App\Vote $vote
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereDescription($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereDetail($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereGrade($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereKeyWord($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereLength($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereNameEn($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereNameZh($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereOfficialUrl($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie wherePlaywright($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie wherePublishAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Movie whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Movie extends Model
{
    protected $fillable = ['name_zh','name_en','playwright','grade','length','official_url','score','vote','detail','publish_at'
        ];
    public function episodes() {
        return $this->hasMany('App\Episode');
    }
    public function vote() {
        return $this->hasOne('App\Vote');
    }
    public function getFullNameAttribute() {
        return $this->name_en.'|'.$this->name_zh;
    }

    public function images() {
        return $this->belongsToMany('App\Image');

    }
    public function articles() {
        return $this->belongsToMany('App\Article');
    }

    public function getUrlsAttribute() {
        return $this->images()->pluck('fname');
    }
    public function setNameEnAttribute($value) {
        $value = preg_replace('#[\.]#',' ',$value);
        $this->attributes['name_en'] = trim($value);
    }
    public function setNameZhAttribute($value) {
        $value = preg_replace('#[\.]#',' ',$value);
        $this->attributes['name_zh'] = trim($value);
    }

    public function getCoverageImageAttribute() {
        $image = $this->images()->first();
        $width =200;
        $height = 140;
        $baseUrl = config('app.QNImageUrl');
        $waterMaker = 'imageView2/1/w/'.$width.'/h/'.$height;
        $key = '1464084303';
        if ($image) {
            return $image->imgURL();
        }
        return $baseUrl.'/'.$key.'?'.$waterMaker;
    }

    public function getSiteUrlAttribute() {
        return config('app.APP_URL').'/movie/'.$this->id;
    }

    public function setDetailAttribute($value){
        $this->attributes['detail'] = str_ireplace('©豆瓣','',$value);
    }

    public function getScoreAttribute() {
        if($this->vote) {
            return $this->vote->score;
        } else {
            return 0.00;
        }
    }
}
